package com.jasiri.project.utils.dialog

import android.Manifest
import android.content.Context
import android.os.Build
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import com.jasiri.lib.permission.OnPermissionCallback
import com.jasiri.lib.permission.XXPermissions
import com.jasiri.project.R
import com.jasiri.project.app.App
import com.jasiri.project.databinding.DialogPhotoBinding
import com.jasiri.project.pro.ProDialog
import com.jasiri.project.utils.event.EventType
import com.jasiri.project.utils.http.HttpHelper

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 拍照、相册选择 dialog
 */
class PhotoDialog: ProDialog {
    private lateinit var binding: DialogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""
    //是否是相机
    private var isCamera = true
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun initWindow() {
        val params = window?.attributes
        params?.width = App.getPhoneWidth()
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    override fun getRootView(): View {
        binding = DialogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        isCamera = true
        onCheckType()
    }

    override fun initListener() {
        setOnClickListener(R.id.btnConfirm) //确定
        binding.cameraLayout.setOnClickListener {
            isCamera = true
            onCheckType()
        }

        binding.albumLayout.setOnClickListener {
            isCamera = false
            onCheckType()
        }
    }

    override fun initData() {
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.btnConfirm -> {
                if (isCamera){//相机
                    val permissions = arrayListOf(Manifest.permission.CAMERA)
                    if (XXPermissions.isGranted(context,permissions)){
                        if(mCallback != null){
                            mCallback?.openCamera()
                        }
                    } else {
                        XXPermissions.with(context)
                            // 申请多个权限
                            .permission(permissions)
                            .request(object:
                                OnPermissionCallback {
                                override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                                    var permissionString = ""
                                    p0.forEach {
                                        permissionString = "$permissionString$it,"
                                    }
                                    HttpHelper.addEvent(EventType.CLICK.CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                                    if(mCallback != null){
                                        mCallback?.openCamera()
                                    }
                                }
                                override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
                                    var permissionString = ""
                                    p0.forEach {
                                        permissionString = "$permissionString$it,"
                                    }
                                    HttpHelper.addEvent(EventType.CLICK.CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
                                    XXPermissions.startPermissionActivity(context,permissions)
                                }
                            })
                    }
                } else {
                    if(mCallback != null){
                        mCallback?.openAlbum()
                    }
                }
            }
        }
    }

    /**
     * 切换
     */
    private fun onCheckType(){
        binding.cameraLayout.setBackgroundResource(R.drawable.radius_fill_fff0f0f0_12)
        binding.imgCamera.setImageResource(R.mipmap.img_ocr_camera_normal)
        binding.textCamera.setTextColor(ContextCompat.getColor(context,R.color.FF6D6D6D))
        binding.imgCameraSelect.setImageResource(R.mipmap.img_ocr_photo_normal)

        binding.albumLayout.setBackgroundResource(R.drawable.radius_fill_fff0f0f0_12)
        binding.imgAlbum.setImageResource(R.mipmap.img_ocr_album_normal)
        binding.textAlbum.setTextColor(ContextCompat.getColor(context,R.color.FF6D6D6D))
        binding.imgAlbumSelect.setImageResource(R.mipmap.img_ocr_photo_normal)

        if (isCamera){
            binding.cameraLayout.setBackgroundResource(R.drawable.ocr_photo_select)
            binding.imgCamera.setImageResource(R.mipmap.img_ocr_camera_select)
            binding.textCamera.setTextColor(ContextCompat.getColor(context,R.color.FF4C8FFB))
            binding.imgCameraSelect.setImageResource(R.mipmap.img_ocr_photo_select)
        } else {
            binding.albumLayout.setBackgroundResource(R.drawable.ocr_photo_select)
            binding.imgAlbum.setImageResource(R.mipmap.img_ocr_album_select)
            binding.textAlbum.setTextColor(ContextCompat.getColor(context,R.color.FF4C8FFB))
            binding.imgAlbumSelect.setImageResource(R.mipmap.img_ocr_photo_select)
        }
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PHOTO_DISMISS,eventRemark)
    }
}